import { Inject, Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'
import { DISCORDOO_OPTIONS } from './discordoo.constants'
import { DiscordooModuleOptions } from './interfaces/discordoo-options.interface'
import { Client, createApp } from 'discordoo'
import { CustomRestProvider } from '../../../apps/bot/src/services/custom-rest.provider'

@Injectable()
export class DiscordooService implements OnModuleInit, OnModuleDestroy {
  constructor(
    @Inject(DISCORDOO_OPTIONS) private readonly options: DiscordooModuleOptions,
  ) {
    const builder = createApp(this.options.discordToken)
      .restProvider(CustomRestProvider)
    this.options.build(builder)
    this._client = builder.build()
  }

  private readonly _client: Client

  async onModuleDestroy() {
    return this.client.destroy()
  }

  get client() {
    return this._client
  }

  async onModuleInit() {
    await this.client.start()
  }
}